package com.whnk.pm.interceptor;

import com.whnk.pm.domain.*;
import com.whnk.pm.enums.VerifyEnum;
import com.whnk.pm.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.regex.Pattern;

@Component
public class UserAuthInterceptor implements HandlerInterceptor {

    @Autowired
    private IBaseInfoService baseInfoService;

    @Autowired
    private IEduInfoService eduInfoService;

    @Autowired
    private IWorkInfoService workInfoService;

    @Autowired
    private IProjInfoService projInfoService;

    @Autowired
    private IFileInfoService fileInfoService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        String empId = "";
        /**
         * 教育经历
         */
        if (StringUtils.startsWith(uri, "/pm/baseInfo/upEdu/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/eduInfo/list")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/eduInfo/add/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/eduInfo/add")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/eduInfo/edit/")) {
            int lastIndex = uri.lastIndexOf("/");
            String eduId = uri.substring(lastIndex + 1);
            EduInfo eduInfo = eduInfoService.selectEduInfoById(Integer.parseInt(eduId));
            empId = eduInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/eduInfo/edit")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/eduInfo/detail/")) {
            int lastIndex = uri.lastIndexOf("/");
            String eduId = uri.substring(lastIndex + 1);
            EduInfo eduInfo = eduInfoService.selectEduInfoById(Integer.parseInt(eduId));
            empId = eduInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/eduInfo/remove")) {
            String ids = request.getParameter("ids");
            String id = ids.split(";")[0];
            EduInfo eduInfo = eduInfoService.selectEduInfoById(Integer.parseInt(id));
            empId = eduInfo.getEmpId();
        }

        /**
         * 工作经历
         */
        else if (StringUtils.startsWith(uri, "/pm/baseInfo/upWork/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/workInfo/list")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/workInfo/add/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/workInfo/add")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/workInfo/edit/")) {
            int lastIndex = uri.lastIndexOf("/");
            String workId = uri.substring(lastIndex + 1);
            WorkInfo workInfo = workInfoService.selectWorkInfoById(Integer.parseInt(workId));
            empId = workInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/workInfo/edit")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/workInfo/detail/")) {
            int lastIndex = uri.lastIndexOf("/");
            String workId = uri.substring(lastIndex + 1);
            WorkInfo workInfo = workInfoService.selectWorkInfoById(Integer.parseInt(workId));
            empId = workInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/workInfo/remove")) {
            String ids = request.getParameter("ids");
            String id = ids.split(";")[0];
            WorkInfo workInfo = workInfoService.selectWorkInfoById(Integer.parseInt(id));
            empId = workInfo.getEmpId();
        }

        /**
         * 项目经历
         */
        else if (StringUtils.startsWith(uri, "/pm/baseInfo/upProj/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/projInfo/list")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/projInfo/add/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/projInfo/add")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/projInfo/edit/")) {
            int lastIndex = uri.lastIndexOf("/");
            String projId = uri.substring(lastIndex + 1);
            ProjInfo projInfo = projInfoService.selectProjInfoById(Integer.parseInt(projId));
            empId = projInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/projInfo/edit")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/projInfo/detail/")) {
            int lastIndex = uri.lastIndexOf("/");
            String projId = uri.substring(lastIndex + 1);
            ProjInfo projInfo = projInfoService.selectProjInfoById(Integer.parseInt(projId));
            empId = projInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/projInfo/remove")) {
            String ids = request.getParameter("ids");
            String id = ids.split(";")[0];
            ProjInfo projInfo = projInfoService.selectProjInfoById(Integer.parseInt(id));
            empId = projInfo.getEmpId();
        }

        /**
         * 附件信息
         */
        else if (StringUtils.startsWith(uri, "/pm/baseInfo/upFile/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/fileInfo/list")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.startsWith(uri, "/pm/fileInfo/add/")) {
            int lastIndex = uri.lastIndexOf("/");
            empId = uri.substring(lastIndex + 1);
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/fileInfo/add")) {
            empId = request.getParameter("empId");
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/fileInfo/remove")) {
            String ids = request.getParameter("ids");
            String id = ids.split(";")[0];
            FileInfo fileInfo = fileInfoService.selectFileInfoById(Integer.parseInt(id));
            empId = fileInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/fileInfo/dlFile")) {
            String id = request.getParameter("fileId");
            FileInfo fileInfo = fileInfoService.selectFileInfoById(Integer.parseInt(id));
            empId = fileInfo.getEmpId();
        } else if (StringUtils.equalsIgnoreCase(uri, "/pm/fileInfo/dlFilesForEmp")) {
            empId = request.getParameter("empId");
        }

        if (StringUtils.isNotBlank(empId)) {
            BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
            if (baseInfo != null) {
                if (StringUtils.equalsIgnoreCase(VerifyEnum.VERIFY_NO_ENUM.getCode(), baseInfo.getEmpVerifyStatus())) {
                    DataThreadLocal.setDataPrivileges(true);
                } else {
                    DataThreadLocal.setDataPrivileges(false);
                }
            }
        }
        return true;
    }
}
